﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.TuyenSinh
{
    public partial class frmThongBaoNhapHoc : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "tuyen_sinh";

        public frmThongBaoNhapHoc()
        {
            InitializeComponent();
        }

        private void frmThongBaoNhapHoc_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoDaoTao' table. You can move, or remove it, as needed.
            this.mTrinhDoDaoTaoTableAdapter.Fill(this.qLTHDataSet.mTrinhDoDaoTao);
            // TODO: This line of code loads data into the 'qLTHDataSet.mNghe' table. You can move, or remove it, as needed.
            this.mNgheTableAdapter.Fill(this.qLTHDataSet.mNghe);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDoiTuongUuTien' table. You can move, or remove it, as needed.
            this.mDoiTuongUuTienTableAdapter.Fill(this.qLTHDataSet.mDoiTuongUuTien);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDonViQuanDoi' table. You can move, or remove it, as needed.
            this.mDonViQuanDoiTableAdapter.Fill(this.qLTHDataSet.mDonViQuanDoi);
            // TODO: This line of code loads data into the 'qLTHDataSet.tDanhSachDuTuyen_getallwithdetail' table. You can move, or remove it, as needed.
            this.tDanhSachDuTuyen_getallwithdetailTableAdapter.Fill(this.qLTHDataSet.tDanhSachDuTuyen_getallwithdetail);

            xInit();
        }

        private bool xInit()
        {
            bool blnResult = false;
            try
            {
                //default ngày gửi là hôm nay
                dtpNgayGui.EditValue = DateTime.Today;

                //list năm tuyển sinh là năm sau và 10 năm trc đó
                cbNamTuyenSinh.Properties.Items.Add(DateTime.Today.Year + 1);
                for (int i = 0; i <= 10; i++)
                {
                    cbNamTuyenSinh.Properties.Items.Add(DateTime.Today.Year - i);
                }
                cbNamTuyenSinh.SelectedIndex = 1;

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            try
            {
                int[] intDs;
                int intTempID;
                object[] objValue = new object[dgvDS.Columns.Count];

                intDs = dgvDsDuTuyen.GetSelectedRows();

                for (int i = intDs.Length - 1; i >= 0; i--)
                {
                    //kiểm tra trường có hợp lệ ko. nếu ko thì thoát                    
                    intTempID = clsCommon.fncCnvNullToInt(dgvDsDuTuyen.GetRowCellValue(intDs[0], colHS_ID));
                    if (intTempID <= 0) return;

                    //thêm dòng mới grid phải
                    for (int j = 0; j <= dgvDS.Columns.Count - 1; j++)
                    {
                        objValue[j] = dgvDsDuTuyen.GetRowCellValue(intDs[i], dgvDS.Columns[j].FieldName);
                    }

                    //xóa dòng bên grid trái
                    dgvDsDuTuyen.DeleteRow(intDs[i]);
                    dgvDS.Nodes.Add(objValue);

                }

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnLoai_Click(object sender, EventArgs e)
        {
            try
            {
                //Làm ngược so với sự kiện btnChon_Click
                int intNewRowIndex;

                DevExpress.XtraTreeList.Nodes.TreeListNode objNode;
                objNode = dgvDS.FocusedNode;
                if (objNode == null) return;

                dgvDsDuTuyen.AddNewRow();
                intNewRowIndex = dgvDsDuTuyen.FocusedRowHandle;

                dgvDsDuTuyen.SetFocusedRowCellValue("TD_ID", objNode.GetValue(TD_ID));
                dgvDsDuTuyen.SetFocusedRowCellValue("NamTuyenSinh", objNode.GetValue(NamTuyenSinh));
                dgvDsDuTuyen.SetFocusedRowCellValue("DotTuyenSinh", objNode.GetValue(DotTuyenSinh));
                dgvDsDuTuyen.SetFocusedRowCellValue("NG_ID1", objNode.GetValue(NG_ID1));
                dgvDsDuTuyen.SetFocusedRowCellValue("NG_ID2", objNode.GetValue(NG_ID2));
                dgvDsDuTuyen.SetFocusedRowCellValue("HS_ID", objNode.GetValue(colHS_ID1));
                dgvDsDuTuyen.SetFocusedRowCellValue("hs_hodem", objNode.GetValue(colhs_hodem1));
                dgvDsDuTuyen.SetFocusedRowCellValue("hs_ten", objNode.GetValue(colhs_ten1));
                dgvDsDuTuyen.SetFocusedRowCellValue("hs_ngaysinh", objNode.GetValue(colhs_ngaysinh1));
                dgvDsDuTuyen.SetFocusedRowCellValue("DV_ID", objNode.GetValue(colDV_ID1));
                dgvDsDuTuyen.SetFocusedRowCellValue("hs_hokhau", objNode.GetValue(colhs_hokhau));
                dgvDsDuTuyen.UpdateCurrentRow();
                dgvDS.DeleteNode(objNode);

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void frmThongBaoNhapHoc_Resize(object sender, EventArgs e)
        {
            try
            {
                //set lại độ rộng 2 grid khi full screen
                int intChange = btnChon.Location.X - 227;   //tọa độ mới(btnChon.Location.X) - 227 là tọa độ X của nút btnChon
                grpDanhSachIn.Width += intChange;
                grpDsDuTuyen.Width += intChange;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (!validControlBlank.Validate()) return;

                Cursor.Current = Cursors.WaitCursor;
                
                DataTable objData;
                DataRow objRow;
                DateTime dtNgayGui;
                string strDotTuyenSinh;
                int intNamTuyenSinh;
                DateTime dtNgayNhapHoc;

                objData = new DataTable();
                objData.Columns.Add("NgayHnay");
                objData.Columns.Add("ThangHnay");
                objData.Columns.Add("NamHnay");
                objData.Columns.Add("DotHoc");
                objData.Columns.Add("NamHoc");
                objData.Columns.Add("HoTen");
                objData.Columns.Add("QueQuan");
                objData.Columns.Add("NgayHoc");

                dtNgayGui = clsCommon.fncCnvNullToDatetime(dtpNgayGui.EditValue);
                strDotTuyenSinh = clsCommon.fncCnvNullToString(cbDotTuyenSinh.Text);
                intNamTuyenSinh = clsCommon.fncCnvNullToInt(cbNamTuyenSinh.Text);
                dtNgayNhapHoc = clsCommon.fncCnvNullToDatetime(dtpNgayNhapHoc.EditValue);

                for (int i = 0; i <= dgvDS.Nodes.Count - 1; i++)
                {
                    objRow = objData.NewRow();
                    objRow["NgayHnay"] = dtNgayGui.Day;
                    objRow["ThangHnay"] = dtNgayGui.Month;
                    objRow["NamHnay"] = dtNgayGui.Year;
                    objRow["DotHoc"] = strDotTuyenSinh;
                    objRow["NamHoc"] = intNamTuyenSinh;
                    objRow["HoTen"] = clsCommon.fncCnvNullToString(dgvDS.Nodes[i].GetValue(colhs_hodem1)) + " " + clsCommon.fncCnvNullToString(dgvDS.Nodes[i].GetValue(colhs_ten1));
                    objRow["QueQuan"] = dgvDS.Nodes[i].GetValue(colhs_hokhau);
                    objRow["NgayHoc"] = dtNgayNhapHoc.ToString("dd/MM/yyyy");

                    objData.Rows.Add(objRow);
                }

                //xuat word
                Export.frmExportThongBaoNhapHoc frmNhapHoc = new Export.frmExportThongBaoNhapHoc();
                frmNhapHoc.fncExport(objData);
                frmNhapHoc.MdiParent = this.ParentForm;
                frmNhapHoc.Show();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }
    }
}